function [signal] = PAUSSignalSpectrumFromSignal(signal_extract, modality, channels, valid_data_only, range, method)
% 从提取的信号为输入

% signal_output = PAUSSignalSpectrumFromSignal(signal_extract,0,[1 16 32 48 64],1,8021:8520,'amplitude_frequency');
% signal_output = PAUSSignalSpectrumFromSignal(signal_extract,1,[1 16 32 48 64],1,4011:4510,'amplitude_frequency');

[p,q]=size(signal_extract.signals);

if valid_data_only == 1
    analysis_signals_length = range(1):range(2); %信号范围
elseif valid_data_only == 0
    analysis_signals_length = 1:p;
    range = [1, p];
end


FFTLength = 10e5;
addzerosfortestmultisignal=zeros((FFTLength-length(signal_extract.signals)),q);
testmultisignal=[signal_extract.signals;addzerosfortestmultisignal];
[m,n]=size(testmultisignal);
fftresult=zeros(m,n);

for Numofsignals=1:n
    fftresult(:,Numofsignals)=fft(testmultisignal(:,Numofsignals),length(testmultisignal(:,1)));
end
Th=0.02;

for Numofsignals=1:n

    absY = (abs(fftresult(:,Numofsignals)).*2/(length(analysis_signals_length)));
    tempRef_Powerspectrum(:,Numofsignals)=10*log10((absY(1:FFTLength/2)));


    THETAY=angle((fftresult(:,Numofsignals)));


    thetadex=find(absY(1:FFTLength/2)<Th);
    THETAfixedY=THETAY(1:FFTLength/2);
    THETAfixedY(thetadex)=0;

    tempRef_THETAfixedY(:,Numofsignals)=unwrap(THETAfixedY);
end

Ref_Powerspectrum(:,1)=mean(tempRef_Powerspectrum(:,1:length(channels)),2);

Ref_THETAfixedY(:,1)=mean(tempRef_THETAfixedY(:,1:length(channels)),2);



if modality == 0

    % analysis_signals_length=8021:8520;  %信号范围

    signal.title = ['US_',method];


    switch method
        case 'time_domain'
            signal.signals = signal_extract.signals;
            XAxisLabel = xlabel('Point','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            signal.XAxisTickLabel = range;
            YAxisLabel = ylabel('Amplitude (a. u.)','fontname', 'times new roman','fontSize',30);
            min_signal_value = min(signal.signals);
            max_signal_value = max(signal.signals);
            delta = (max_signal_value - min_signal_value) * 0.05;
            signal.YAxisTickLabel = [min_signal_value-delta,  max_signal_value+delta];     %y轴刻度
        case 'hilbert_envelope'
            signal.signals = abs(hilbert(signal_extract.signals));
            XAxisLabel = xlabel('Point','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            signal.XAxisTickLabel = range;
            YAxisLabel = ylabel('Amplitude (a. u.)','fontname', 'times new roman','fontSize',30);
            min_signal_value = min(signal.signals);
            max_signal_value = max(signal.signals);
            delta = (max_signal_value - min_signal_value) * 0.05;
            signal.YAxisTickLabel = [min_signal_value-delta,  max_signal_value+delta];     %y轴刻度
        case 'amplitude_frequency'
            signal.signals = Ref_Powerspectrum(1:30000,1)-repmat(max(Ref_Powerspectrum(1:30000,1)),30000,1);
            XAxisLabel = xlabel('Frequency (Hz)','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            YAxisLabel = ylabel('NPSD (dB)','fontname', 'times new roman','fontSize',30);
            xlim([1 6.0e6]);
            signal.XAxisTickLabel = xlim;
            ylim([-15 0]);
            signal.YAxisTickLabel = ylim;
        case 'phase_frequency'
            signal.signals = Ref_THETAfixedY(1:30000,1);
            xlim([1 6.0e6]);
            signal.XAxisTickLabel = xlim;
            ylim([-40 -0]);
            signal.YAxisTickLabel = ylim;
            XAxisLabel = xlabel('Frequency (Hz)','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            YAxisLabel = ylabel('Phase (Angle)','fontname', 'times new roman','fontSize',30);
    end

    signal.XAxisLabel = XAxisLabel.String;
    signal.YAxisLabel = YAxisLabel.String;
    signal.pointnumber = length(analysis_signals_length);
    signal.selectedchannels = channels;
    close(gcf);

%===============Boyi Li PA spectrum analysis 20230401====================&
elseif modality == 1

    signal.title = ['PA_',method];


    switch method
        case 'time_domain'
            signal.signals = signal_extract.signals;
            XAxisLabel = xlabel('Point','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            signal.XAxisTickLabel = range;
            YAxisLabel = ylabel('Amplitude (a. u.)','fontname', 'times new roman','fontSize',30);
            min_signal_value = min(signal.signals);
            max_signal_value = max(signal.signals);
            delta = (max_signal_value - min_signal_value) * 0.05;
            signal.YAxisTickLabel = [min_signal_value-delta,  max_signal_value+delta];     %y轴刻度
        case 'hilbert_envelope'
            signal.signals = abs(hilbert(signal_extract.signals));
            XAxisLabel = xlabel('Point','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            signal.XAxisTickLabel = range;
            YAxisLabel = ylabel('Amplitude (a. u.)','fontname', 'times new roman','fontSize',30);
            min_signal_value = min(signal.signals);
            max_signal_value = max(signal.signals);
            delta = (max_signal_value - min_signal_value) * 0.05;
            signal.YAxisTickLabel = [min_signal_value-delta,  max_signal_value+delta];     %y轴刻度
        case 'amplitude_frequency'
            signal.signals = Ref_Powerspectrum(1:30000,1)-repmat(max(Ref_Powerspectrum(1:30000,1)),30000,1);
            XAxisLabel = xlabel('Frequency (Hz)','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            YAxisLabel = ylabel('NPSD (dB)','fontname', 'times new roman','fontSize',30);
            xlim([1 6.0e6]);
            signal.XAxisTickLabel = xlim;
            ylim([-15 0]);
            signal.YAxisTickLabel = ylim;
        case 'phase_frequency'
            signal.signal = Ref_THETAfixedY(1:30000,1);
            xlim([1 6.0e6]);
            signal.XAxisTickLabel = xlim;
            ylim([-25 -0]);
            signal.YAxisTickLabel = ylim;
            XAxisLabel = xlabel('Frequency (Hz)','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            YAxisLabel = ylabel('Phase (Angle)','fontname', 'times new roman','fontSize',30);
    end
    signal.XAxisLabel = XAxisLabel.String;
    signal.YAxisLabel = YAxisLabel.String;
    signal.pointnumber = length(analysis_signals_length);
    signal.selectedchannels = channels;
    close(gcf);

end

end